package handler

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"sbd_zwjy/models"
	"strings"
)

type result struct {
	Success bool        `json:"success"`
	Code    interface{} `json:"code"`
	Message string      `json:"message"`
	Data    Data        `json:"data"`
	Total   int         `json:"total"`
}

type Data struct {
	Sign string `json:"sign"`
}

func GetSignHandler(content string) string {

	var result result

	client := &http.Client{}
	var data = strings.NewReader("app_id=nynctczscxkcxpc&interface_id=nynctcxpc&header=&version=1.0&charset=UTF-8&origin=0&timestamp=1662642131945&" + content)
	req, err := http.NewRequest("POST", "https://tftb.sczwfw.gov.cn:8085/api/jmas-api-gateway-server/createsign.do", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
	req.Header.Set("Accept-Language", "zh,zh-CN;q=0.9,en;q=0.8")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	req.Header.Set("Origin", "https://www.sczwfw.gov.cn")
	req.Header.Set("Referer", "https://www.sczwfw.gov.cn/")
	req.Header.Set("Sec-Fetch-Dest", "empty")
	req.Header.Set("Sec-Fetch-Mode", "cors")
	req.Header.Set("Sec-Fetch-Site", "same-site")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
	req.Header.Set("sec-ch-ua", "\"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"")
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("sec-ch-ua-platform", "\"Windows\"")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	bodyText, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", bodyText)

	err = json.Unmarshal(bodyText, &result)

	if err != nil {
		log.Fatal(err)
	}
	sign := result.Data.Sign

	return sign
}

type Option struct {
	AppID       string `json:"app_id"`
	InterfaceID string `json:"interface_id"`
	Header      string `json:"header"`
	Version     string `json:"version"`
	Charset     string `json:"charset"`
	Origin      string `json:"origin"`
	Timestamp   string `json:"timestamp"`
	Sign        string `json:"sign"`
	BizContent  string `json:"biz_content"`
}

func ReListHandler(sign, content string) (total int, results []models.ResultList) {

	client := &http.Client{}

	var data = strings.NewReader("app_id=nynctczscxkcxpc&interface_id=nynctcxpc&header=&version=1.0&charset=UTF-8&origin=0&timestamp=1662642131945&sign=" +
		sign +
		"&" +
		content)

	req, err := http.NewRequest("POST", "https://tftb.sczwfw.gov.cn:8085/api/jmas-api-gateway-server/gateway.do", data)

	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
	req.Header.Set("Accept-Language", "zh,zh-CN;q=0.9,en;q=0.8")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	req.Header.Set("Origin", "https://www.sczwfw.gov.cn")
	req.Header.Set("Referer", "https://www.sczwfw.gov.cn/")
	req.Header.Set("Sec-Fetch-Dest", "empty")
	req.Header.Set("Sec-Fetch-Mode", "cors")
	req.Header.Set("Sec-Fetch-Site", "same-site")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
	req.Header.Set("sec-ch-ua", "\"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"")
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("sec-ch-ua-platform", "\"Windows\"")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	bodyText, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	str := strings.Replace(string(bodyText), "\\", "", -1)
	str = strings.Replace(str, "检疫单证号", "Code", -1)
	str = strings.Replace(str, "起运（签发）地", "InitAddress", -1)
	str = strings.Replace(str, "发往地", "StartAddress", -1)
	str = strings.Replace(str, "植物名称", "PlantName", -1)
	str = strings.Replace(str, "品种名称", "VarietyName", -1)
	str = strings.Replace(str, "单位或个人", "ApplyCompanyName", -1)
	str = strings.Replace(str, "收货单位联系人", "TakeLink", -1)
	str = strings.Replace(str, "收货单位", "TakeCompany", -1)
	str = strings.Replace(str, "调运数量", "TakeNum", -1)
	str = strings.Replace(str, "签发日期", "IssueDate", -1)
	str = strings.Replace(str, "\"{", "{", -1)
	str = strings.Replace(str, "}\"}", "}}", -1)

	fmt.Println(str)
	var result models.Result

	err = json.Unmarshal([]byte(str), &result)
	if err != nil {
		fmt.Println("json.Unmarshal err of : ", err)
	}
	// fmt.Println(result.Data.Data.List)
	fmt.Printf("ccccccccccccc%+v\n", result.Data.Data.List[0])

	total = result.Data.Data.Total
	results = result.Data.Data.List
	// fmt.Println(result.Data.Data.List[0])
	// fmt.Printf("%s\n", str)
	return
}
